package Algorithms.ch01;

import java.security.MessageDigest;
import java.text.NumberFormat;
import java.util.Arrays;

public class BinarySearch {
    public static int rank(int key, int[] a) {
        int lo = 0;
        int hi = a.length - 1;
        while (lo <= hi) {
            int mid = lo + (hi - lo) / 2; // 向下取整
            if (key < a[mid]) hi = mid - 1; // 缩小上边界
            else if (key > a[mid]) lo = mid + 1; // 缩小下边界
            else return mid;
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] a = {3, 8, 2, 5, 11, 99, 101, 1, 7, 22};
        Arrays.sort(a); // 先排序，后查找
        System.out.println(Arrays.toString(a));
        int i = rank(111, a);
        System.out.println(i);
        NumberFormat f = NumberFormat.getCurrencyInstance();
        System.out.println(f.format(3));
    }
}
